package com.qf.sl.youshare.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.qf.sl.youshare.Constants;
import com.qf.sl.youshare.R;
import com.qf.sl.youshare.model.Product;
import com.qf.sl.youshare.utils.FileCache;
import com.qf.sl.youshare.utils.ImageCache;
import com.qf.sl.youshare.utils.ImageLoader;

import java.util.List;

/**
 * Created by IDEA
 * User : sl
 * on 2015/2/7.
 * 11:15
 */
public class StorePlatformAdapter extends BaseAdapter {

    //1, 设置数据
    private List<Product> products;

    //2, 准备布局
    private LayoutInflater inflater;

    public StorePlatformAdapter(Context context, List<Product> products) {

        if (context == null) {
            throw new IllegalArgumentException("Context must not null");
        }

        inflater = LayoutInflater.from(context);
        this.products = products;
    }

    @Override
    public int getCount() {
        int ret = 0;
        if (products != null) {
            ret = products.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        Object ret = null;
        if (products != null) {
            ret = products.get(position);
        }
        return ret;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;

        //TODO 制作界面

        if (convertView != null) {
            ret = convertView;
        }else {
            ret = inflater.inflate(R.layout.item_store_platform, parent, false);
        }

        ViewHolder holder = (ViewHolder) ret.getTag();

        if (holder == null){
            holder = new ViewHolder();

            holder.iconHolder = (ImageView) ret.findViewById(R.id.item_platform_icon);
            holder.titleHolder = (TextView) ret.findViewById(R.id.item_platform_title);
            holder.priceHolder = (TextView) ret.findViewById(R.id.item_platform_price);

            ret.setTag(holder);
        }

        //获取数据部分

        Product product = products.get(position);

        holder.titleHolder.setText(product.getGoodsName());
        holder.priceHolder.setText(product.getShopPrice());

        //TODO 进行图片的加载

        //1, 获取缩略图地址
        //2, 检查内存地址
        //3, 检查文件缓存
        //4, 网络加载数据
        //5, 更新缓存

        holder.iconHolder.setImageResource(R.drawable.product_default);

        String goodsThumb = product.getGoodsThumb();

        if (goodsThumb != null && goodsThumb.length() > 0) {
            if (!goodsThumb.startsWith("http://") && !goodsThumb.startsWith("https://")){
                goodsThumb = Constants.IMAGE_SERVER + goodsThumb;
            }
            ImageCache imageCache = ImageCache.getInstance();
            Bitmap bitmap = imageCache.getImage(goodsThumb);
            if (bitmap != null) {
                holder.iconHolder.setImageBitmap(bitmap);
            }else {
                // TODO 查看文件缓存

                FileCache fileCache = FileCache.getInstance();
                byte[] bytes = fileCache.getFileContent(goodsThumb);

                if (bytes != null && bytes.length > 0) {
                    bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

                    imageCache.putImage(goodsThumb,bitmap);

                    holder.iconHolder.setImageBitmap(bitmap);
                }else {
                    //TODO 进行网络的下载

                    ImageLoader task = new ImageLoader(holder.iconHolder);

                    task.execute(goodsThumb);
                }

            }
        }

        return ret;
    }

    private static class ViewHolder{
        public ImageView iconHolder;
        public TextView titleHolder;
        public TextView priceHolder;
    }
}
